import java.util.ArrayList;
import java.util.List;

public class UndoTask {

	SearchTask searchTask;
	ViewTask viewTask;
	/**
	 * This method is called to execute the undo operation
	 * @param userInputElements
	 * @param tasksInTempStorage
	 * @param tasksOnDisplay
	 * @param typeTasksOnDisplay
	 * @param snapshotOfTasks
	 */
	public void execute(UserInputElements userInputElements,
			List<Task> tasksInTempStorage, List<Task> tasksOnDisplay,
			String typeTasksOnDisplay, List<List<Task>> snapshotOfTasks) {
		String message;
		String undoParameter = userInputElements.getTaskDescription();
		if (!undoParameter.equals("")) {
			message = "parameter not needed for undo";
		} else if (snapshotOfTasks.size() == 1) {
			message = "cannot undo more than twice";
		}

		else {
			TaskO.taskList.tasksInTempStorage = this.undoFromTempStorage(
					tasksInTempStorage, snapshotOfTasks);
			this.clearCurrentTasksOnDisplay(tasksOnDisplay);
			this.updateDisplay(tasksInTempStorage, tasksOnDisplay,
					typeTasksOnDisplay);
			message = "undo successful";
		}
		TaskO.taskOMainScreen.appendOutput(message);
		System.out.println(message);
	}

	/**
	 * 
	 * @param tasksInTempStorage
	 * @param snapshotOfTasks
	 * @return
	 */
	private List<Task> undoFromTempStorage(List<Task> tasksInTempStorage,
			List<List<Task>> snapshotOfTasks) {

		int snapshotSize = snapshotOfTasks.size();

		tasksInTempStorage = new ArrayList<Task>();
		snapshotOfTasks.remove(snapshotSize - 1);
		tasksInTempStorage = snapshotOfTasks.get(snapshotSize - 2); // magic
																	// numbers
		return tasksInTempStorage;
	}

	/**
	 * 
	 * @param tasksInTempStorage
	 * @param tasksOnDisplay
	 * @param typeTasksOnDisplay
	 */
	private void updateDisplay(List<Task> tasksInTempStorage,
			List<Task> tasksOnDisplay, String typeTasksOnDisplay) {
		String viewParameter = "";

		if (typeTasksOnDisplay.equalsIgnoreCase("/ALL/")
				|| typeTasksOnDisplay.equalsIgnoreCase("/STAR/")
				|| typeTasksOnDisplay.equalsIgnoreCase("/DEADLINE/")
				|| typeTasksOnDisplay.equalsIgnoreCase("/FINISHED/")) {
			
			if(typeTasksOnDisplay.equalsIgnoreCase("/ALL/")) {
				viewParameter="all";
			}
			else if(typeTasksOnDisplay.equalsIgnoreCase("/STAR/")) {
				viewParameter="star";
			}
			else if(typeTasksOnDisplay.equalsIgnoreCase("/DEADLINE/")) {
				viewParameter="deadline";	
				}
			else if(typeTasksOnDisplay.equalsIgnoreCase("/FINISHED/")) {
				viewParameter="finished";
			}
			else {
				;
			}
			this.viewTask = new ViewTask();
			this.viewTask.viewFromTempStorage(viewParameter, tasksInTempStorage, tasksOnDisplay);
		} else {
			this.searchTask = new SearchTask();
			this.searchTask.execute(tasksInTempStorage, tasksOnDisplay,
					typeTasksOnDisplay);
		}

	}

	/**
	 * 
	 * @param tasksOnDisplay
	 */
	// TODO 1 - what do I put here
	private void clearCurrentTasksOnDisplay(List<Task> tasksOnDisplay) {
		try {
			tasksOnDisplay.clear();
		} catch (Exception e) {
			TaskO.taskOMainScreen.appendOutput("the tasks on display may be shown incorrectly");
		}
	}

}
